package com.ideabove.radio.module;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.ideabove.network.NetWorkManager;
import com.ideabove.radio.MainActivity;
import com.ideabove.radio.R;
import com.ideabove.radio.service.PlayBackService;
import com.ideabove.radio.service.PlayBackService.MediaPlayerSingleton;
import com.ideabove.util.Connectivity;

public class NotificationMd {
	MainActivity activity 	= null;
	Thread httpThread = null;

	@SuppressLint("HandlerLeak")
	Handler mMainHandler = new Handler() {

		public void handleMessage(Message msg) {        	 
			AlertDialog.Builder ab=new AlertDialog.Builder(activity);  
			ab.setMessage((String)msg.obj);  
			ab.setPositiveButton("ok", null);  
			ab.show();  
		}

	};

	public void sendMessage(String msg){
		Message mssg = new Message();
		mssg.obj = msg;
		mMainHandler.sendMessage(mssg);
	}

	public void init(MainActivity activity){
		this.activity = activity;
		getNotification();		
	}
	
	public void getNotification(){
		httpThread = new Thread(new Runnable() {
			public void run(){
				InputStream in = null;
				BufferedReader reader = null;
				try {				

					HttpClient client = NetWorkManager.getHttpConnection();    
					HttpConnectionParams.setConnectionTimeout(client.getParams(), 5000);										

					String getURL = "http://14.63.217.155/radio_bonosound/mobileNotice.php";

					HttpGet get = new HttpGet(getURL); 
			        HttpResponse responseGET = client.execute(get);   

//					List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
//					params.add(new BasicNameValuePair("uId", "11"));
//					UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
//					post.setEntity(ent);

					in = responseGET.getEntity().getContent();
					reader = new BufferedReader(new InputStreamReader(in));
					StringBuilder str = new StringBuilder();
					String line = null;
					while((line = reader.readLine()) != null)
					{					
						str.append(line);
					}
					in.close();					
					String jsonStr = str.toString();
					
					Log.e("Notification######", jsonStr);
					
					if("0".equals(jsonStr)){
						
					}else if(jsonStr != null && jsonStr.length() > 0){			
						JSONObject jsonObj = new JSONObject(jsonStr); 
						JSONArray resultArray = jsonObj.getJSONArray("result");

//						Log.e("####", "####" + jsonObj);
						JSONObject jObj = resultArray.getJSONObject(0);
						String content = jObj.getString("content").toString();

						sendMessage(content);						
					}


				}catch(org.json.JSONException je){
					
				}catch(IOException e){	
					if(in != null){
						try {
							in.close();
							
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					
//					getNotification();					
					e.printStackTrace();
				}
				httpThread = null;	
			}
		});

		httpThread.start();
	}


	public void onClick(View v,Messenger mMessenger,Messenger mService ){

		if(!Connectivity.isConnected(activity.getApplicationContext())){
			Toast.makeText(activity.getApplicationContext(), "Network is not available.", Toast.LENGTH_SHORT ).show();
			return;
		}

		MediaPlayerSingleton.whatKindOfNetwork = Connectivity.getWhatKindOfNetwork(activity.getApplicationContext());		
		Message msg = Message.obtain(null, PlayBackService.REQUEST_MSG_GET_BONO_CH_PLAYLIST);
		try{
			switch(v.getId()){
			case R.id.radio_every_music:					
				msg.replyTo = mMessenger;
				msg.arg1 = 1; // type
				mService.send(msg);				
				break;

			case R.id.radio_ch_hotsong:
				msg.replyTo = mMessenger;
				msg.arg1 = 2; // type
				mService.send(msg);	
				break;
			case R.id.radio_ch_kpophotsong:
				msg.replyTo = mMessenger;
				msg.arg1 = 3; // type
				mService.send(msg);
				break;
			case R.id.radio_ch_bonoflowfm:
				msg.replyTo = mMessenger;
				msg.arg1 = 4; // type
				mService.send(msg);
				break;

			}		
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
